package com.jwdf.uda.ui.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.UploadImage;
import com.jwdf.uda.databinding.FragmentGuideInteractionAddBinding;
import com.jwdf.uda.runtime.PermissionRequest;
import com.jwdf.uda.runtime.RuntimePermission;
import com.jwdf.uda.ui.activity.presenter.GuideInteractionAddPresenter;
import com.jwdf.uda.ui.activity.view.IGuideInteractionAddView;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;

import java.util.List;

/**
 * 类说明：服务互动
 */
public class GuideInteractionAddActivity extends AbsTitleBarActivity {
    private final static String EXTRA_GUIDE_ID = "guideId";
    private final static String EXTRA_USER_ID = "userId";

    public static void startActivity(Context context, int guideId, String userId) {
        Intent intent = new Intent(context, GuideInteractionAddActivity.class);
        intent.putExtra(EXTRA_GUIDE_ID, guideId);
        intent.putExtra(EXTRA_USER_ID, userId);
        context.startActivity(intent);
    }

    private int guideId;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        guideId = getIntent().getIntExtra(EXTRA_GUIDE_ID, guideId);
        userId = getIntent().getStringExtra(EXTRA_USER_ID);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putInt(EXTRA_GUIDE_ID, guideId);
        args.putString(EXTRA_USER_ID, userId);
        return args;
    }

    @Override
    protected String getTopTitle() {
        return getString(R.string.title_guide_interaction);
    }

    @Override
    protected void setupTitleView() {
        super.setupTitleView();
        setOperateTitle(getString(R.string.guide_interaction_send));
    }

    @Override
    public void onHeadActionClick(View view) {
        super.onHeadActionClick(view);
        ((GuideInteractionAddFragment) mFragment).onHeadActionClick();
    }

    @Override
    protected BaseFragment onCreateContentFragment() {
        return new GuideInteractionAddFragment();
    }

    public static class GuideInteractionAddFragment extends BaseFragment implements IGuideInteractionAddView {
        private FragmentGuideInteractionAddBinding mBinding;
        private GuideInteractionAddPresenter mPresenter;
        private int guideId;
        private String userId;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_guide_interaction_add, container, false);
            initView();
            return mBinding.getRoot();
        }

        private void initView() {
            checkPermission();
            guideId = getArguments().getInt(EXTRA_GUIDE_ID);
            userId = getArguments().getString(EXTRA_USER_ID);

            mPresenter = new GuideInteractionAddPresenter(this, guideId);
            mBinding.isImages.setClothesSelect(false);
            mBinding.isImages.setUserId(userId);
            EventBusUtil.registerEvent(mBinding.isImages);
        }

        @Override
        public void onDestroyView() {
            EventBusUtil.unregisterEvent(mBinding.isImages);
            super.onDestroyView();
        }

        @PermissionRequest(100)
        private void checkPermission() {
            String perms[] = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
            if (!RuntimePermission.hasPermissions(getContext(), perms)) {
                RuntimePermission.requestPermissions(this, getString(R.string.app_name),
                        100, perms);
            }
        }

        @Override
        public BaseFragment getFragment() {
            return this;
        }

        @Override
        public void onHeadActionClick() {
            mPresenter.onHeadActionClick();
        }


        @Override
        public String getContent() {
            return StringUtils.filterNullAndTrim(mBinding.etContent.getText().toString());
        }

        @Override
        public List<UploadImage> getUploadImageList() {
            return mBinding.isImages.getUploadImageList();
        }

        @Override
        public void setImageToken(UploadImage uploadImage) {
            mBinding.isImages.setImageToken(uploadImage);
        }

        @Override
        public String getDesignerId() {
            return null;
        }

    }
}
